package com.link.base.base.clue.service;

import com.link.base.basic.service.LovService;
import com.link.base.base.accnt.dao.mybatis.mapper.AccountMapper;
import com.link.base.base.accnt.model.Account;
import com.link.base.base.clue.dao.mybatis.mapper.ClueMapper;
import com.link.base.base.clue.model.Clues;
import com.link.base.base.contacts.dao.mybatis.mapper.ContactsMapper;
import com.link.base.base.contacts.model.Contacts;
import com.link.core.basic.dao.KeyGenerateDao;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author LiYang
 * @version 1.0
 * @date: 2018/11/8 16:51
 */

@Service
public class ClueServiceImpl extends BasicServiceImpl<Clues> implements ClueService {

    @Resource
    private ClueMapper clueMapper;

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private ContactsMapper contactsMapper;

    @Resource
    private LovService lovService;

    @Resource
    private KeyGenerateDao keyGenerateDao;

    /**
     * 重写父类方法
     */
    @Override
    public BasicMapper<Clues> getBasicMapper() throws Exception {
        if (clueMapper != null) {
            return clueMapper;
        }
        throw new ServiceException("PUBLIC-006");
    }

    /**
     * 跟进线索：根据Id跟进线索
     *
     * @param c
     * @throws Exception
     */
    @Override
    public void clueFollow(Clues c) throws Exception {
        Clues result = queryById(c);
        String newValue = "New";
        String clueFollowStatus = "CLUE_FOLLOW_STATUS";
        if (newValue.equals(lovService.getLovValue(clueFollowStatus, result.getFollowStatus()))
                || "UNTREATED".equals(lovService.getLovValue("CLUE_FOLLOW_STATUS", result.getFollowStatus()))) {
            // 跟进线索
            clueMapper.clueFollowUpdate(c);
        } else {
            throw new ServiceException("CLUE-001",
                    lovService.getLovName("CLUE_FOLLOW_STATUS", "New"),
                    lovService.getLovName("CLUE_FOLLOW_STATUS", "UNTREATED"));
        }
    }

    /**
     * 撤回线索：根据Id撤回线索
     *
     * @param c
     * @throws Exception
     */
    @Override
    public void clueRecall(Clues c) throws Exception {
        // 撤回线索
        clueMapper.clueRecallUpdate(c);
    }

    /**
     * 完成线索：根据Id完成线索
     *
     * @param c
     * @throws Exception
     */
    @Override
    public void clueFinish(Clues c) throws Exception {
        // 完胜线索
        clueMapper.clueFinishUpdate(c);
    }

    /**
     * 取消线索：根据Id取消线索
     *
     * @param c
     * @throws Exception
     */
    @Override
    public void clueCancel(Clues c) throws Exception {
        // 取消线索
        clueMapper.clueCancelUpdate(c);
    }

    /**
     * 线索生成客户
     *
     * @param c
     * @throws Exception
     */
    @Override
    public void clueToAccount(Clues c) throws Exception {

        Account account = new Account();
        account.setCountry("中国");
        account.setAcctType("普通客户");
        account.setAcctStatus("正常");
        account.setCurrency("人民币");
        account.setAcctName(c.getName());
        account.setProvince(c.getProvince());
        account.setCity(c.getCity());
        account.setDistrict(c.getDistrict());
        account.setAddress(c.getAddress());
        account.setPostnId(c.getPostnId());
        account.setOrgId(c.getOrgId());
        String contactName = c.getContactName();
        String mobilePhone = c.getMobilePhone();
        Long accntId = keyGenerateDao.keyGenerate();
        Long contactId;
        // 线索内容填写了联系人，则新建联系人，并将其与客户关联
        if (!"".equals(contactName) && contactName != null) {
            contactId = keyGenerateDao.keyGenerate();
            Contacts contact = new Contacts();
            contact.setId(contactId);
            contact.setAcctId(accntId);
            contact.setContactsName(contactName);
            contact.setMobilePhone(mobilePhone);
            contact.setIsEffective("Y");
            account.setContactId(contactId);
            contactsMapper.insert(contact);
        }
        account.setId(accntId);
        account.setAcctCode(String.valueOf(accntId));
        accountMapper.insert(account);
    }
}
